/**
 * 
 */
package com.isaac.actm.ui.struts2;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.isaac.actm.domain.pojo.User;
import com.isaac.actm.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

/**
 * @author ybak
 * 
 */
public class UserManagement extends ActionSupport implements ModelDriven<User> {

	private static final long serialVersionUID = -2584487048016941330L;
	private UserService service;

	private User user = new User();
	private Long userID;
	private List<User> users;

	@Override
	@SkipValidation
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "user.name", key = "error.missing.username"),
			@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "user.password", key = "error.missing.password"),
			@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "user.confirmPassword", key = "error.missing.confirmPassword") })
	@FieldExpressionValidator(expression = "user.password == user.confirmPassword", key = "error.mismatch.password")
	public String save() {
		service.saveUser(user);
		return SUCCESS;
	}

	@SkipValidation
	public String list() {
		users = service.getUsers();
		return SUCCESS;
	}

	@SkipValidation
	public String edit() {
		user = service.getUserByID(userID);
		user.setConfirmPassword(user.getPassword());
		return SUCCESS;
	}

	@SkipValidation
	public String delete() {
		service.deleteUser(user);
		return SUCCESS;
	}

	public User getModel() {
		return user;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Long getUserID() {
		return userID;
	}

	public void setUserID(Long userID) {
		this.userID = userID;
	}

	public UserService getService() {
		return service;
	}

	public void setService(UserService service) {
		this.service = service;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}
